﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using InstrumentCheck.Module;
using InstrumentCheck.SQLHelper;
using System.Data;
using System.Data.SqlClient;

namespace InstrumentCheck.DAL
{
     public class Form_dal
    {
         public Form_dal()
         { }

         public void SendForm(ApplyForm form)
         {
             SqlConnection cn = Helper.GetConnection();
             SqlCommand cmd = new SqlCommand();
             SqlTransaction trans = null;

             try
             {
                 Helper.OpenConnection(cn);
                 trans = cn.BeginTransaction();
                 cmd.Connection = cn;
                 cmd.Transaction = trans;

                 SaveFormInfo_dal(form, cmd);
                 SaveAttachment(form, cmd);
                 trans.Commit();
             }
             catch
             {
                 trans.Rollback();
                 throw;
             }
             finally
             {
                 if (cmd != null)
                 {
                     cmd.Dispose();
                     cmd = null;
                 }

                 if (cn != null)
                     Helper.CloseConnection(cn);
             }
         }
         public void SaveForm(ApplyForm form)
         {
             SqlConnection cn = Helper.GetConnection();
             SqlCommand cmd = new SqlCommand();
             SqlTransaction trans = null;

             try
             {
                 Helper.OpenConnection(cn);
                 trans = cn.BeginTransaction();
                 cmd.Connection = cn;
                 cmd.Transaction = trans;

                 form.FormNO = 0;
                 form.FormStatus = FormStatus.Draft;
                 SaveFormInfo_dal(form, cmd);

                 trans.Commit();
             }
             catch
             {
                 trans.Rollback();
                 throw;
             }
             finally
             {
                 if (cmd != null)
                 {
                     cmd.Dispose();
                     cmd = null;
                 }
                 if (cn != null)
                     Helper.CloseConnection(cn);
             }
         }
         public void ApproveForm(ApplyForm form, FormRouting routing)
         {
             //根據routing的FlowNO來判斷是否需要更新表單資料，也可以不經過判斷直接更新
             SqlConnection cn = Helper.GetConnection();
             SqlCommand cmd = new SqlCommand();
             SqlTransaction trans = null;

             try
             {
                 Helper.OpenConnection(cn);
                 trans = cn.BeginTransaction();
                 cmd.Connection = cn;
                 cmd.Transaction = trans;

                 if (routing.FlowNO > 10)
                     SaveFormInfo_dal(form, cmd);

                 //如果是30，可能user有新上传附件或者有修改附件，所以应该对附件table进行修改，先删除然后再insert
                 if (routing.FlowNO == 30)
                 {
                     DeleteAttachment(form.FormNO, cmd);
                     SaveAttachment(form, cmd);
                 }

                 //更新流程信息
                 UpdateRouting(routing, cmd);

                 trans.Commit();
             }
             catch
             {
                 trans.Rollback();
                 throw;
             }
             finally
             {
                 if (cmd != null)
                 {
                     cmd.Dispose();
                     cmd = null;
                 }

                 if (cn != null)
                     Helper.CloseConnection(cn);
             }
         }
         public void Cancel(ApplyForm form)
         {
             SqlConnection cn = Helper.GetConnection();
             SqlCommand cmd = new SqlCommand();
             SqlTransaction trans = null;

             try
             {
                 Helper.OpenConnection(cn);
                 trans = cn.BeginTransaction();
                 cmd.Connection = cn;
                 cmd.Transaction = trans;

                 SqlParameter[] para = new SqlParameter[1];
                 para[0] = SQLHelper.SQLHelper.CreateParameter("FormNO", SqlDbType.Int, form.FormNO, ParameterDirection.Input);
                 SQLHelper.SQLHelper.ExecuteNonQuery(CommandType.StoredProcedure, "sp_CancelForm", para);

                 trans.Commit();
             }
             catch
             {
                 trans.Rollback();
                 throw;
             }
             finally
             {
                 if(cmd != null)
                 {
                     cmd.Dispose();
                     cmd = null;
                 }
                 if (cn != null)
                     Helper.CloseConnection(cn);
             }
         }
         public void Reject(FormRouting routing, int intRejectTo)
         {
             SqlConnection cn = Helper.GetConnection();
             SqlCommand cmd = new SqlCommand();
             SqlTransaction trans = null;

             try
             {
                 Helper.OpenConnection(cn);
                 trans = cn.BeginTransaction();
                 cmd.Connection = cn;
                 cmd.Transaction = trans;

                 RejectRouting(routing, intRejectTo, cmd);

                 trans.Commit();
             }
             catch
             {
                 trans.Rollback();
                 throw;
             }
             finally
             {
                 if (cmd != null)
                 {
                     cmd.Dispose();
                     cmd = null;
                 }
                 if (cn != null)
                     Helper.CloseConnection(cn);
             }
         }

         public void DeleteAttachment(int intFormNO, SqlCommand cmd)
         {
             string sql = "delete from tbl_attachment where FormNO = @FormNO";
             cmd.Parameters.Clear();

             cmd.Parameters.Add(new SqlParameter("FormNO", SqlDbType.Int));
             cmd.Parameters["FormNO"].Value = intFormNO;

             cmd.CommandText = sql;
             cmd.CommandType = CommandType.Text;
             cmd.ExecuteNonQuery();
         }
         public void SaveFormInfo_dal(ApplyForm form,SqlCommand cmd)
         {
             cmd.Parameters.Clear();
             cmd.Parameters.Add(new SqlParameter("InstanceID",SqlDbType.UniqueIdentifier));
             cmd.Parameters.Add(new SqlParameter("CorrectID", SqlDbType.Int));
             cmd.Parameters.Add(new SqlParameter("Number", SqlDbType.Int));
             cmd.Parameters.Add(new SqlParameter("Creater", SqlDbType.VarChar));
             cmd.Parameters.Add(new SqlParameter("Applicant", SqlDbType.VarChar));
             cmd.Parameters.Add(new SqlParameter("Create_date", SqlDbType.DateTime));
             cmd.Parameters.Add(new SqlParameter("Type", SqlDbType.Int));
             cmd.Parameters.Add(new SqlParameter("Reason", SqlDbType.NVarChar));
             cmd.Parameters.Add(new SqlParameter("Fas_Type", SqlDbType.Int));
             cmd.Parameters.Add(new SqlParameter("Fas_NO",SqlDbType.VarChar));
             cmd.Parameters.Add(new SqlParameter("PR_NO", SqlDbType.VarChar));
             cmd.Parameters.Add(new SqlParameter("Buy_Date", SqlDbType.DateTime));
             cmd.Parameters.Add(new SqlParameter("Buy_Currency", SqlDbType.VarChar));
             cmd.Parameters.Add(new SqlParameter("Buy_AMT", SqlDbType.Float));
             cmd.Parameters.Add(new SqlParameter("First_Correct", SqlDbType.DateTime));
             cmd.Parameters.Add(new SqlParameter("Correct_Date", SqlDbType.DateTime));
             cmd.Parameters.Add(new SqlParameter("Exp_Currency", SqlDbType.VarChar));
             cmd.Parameters.Add(new SqlParameter("Correct_AMT", SqlDbType.Float));
             cmd.Parameters.Add(new SqlParameter("Correct_Period", SqlDbType.Int));
             cmd.Parameters.Add(new SqlParameter("Status", SqlDbType.Int));
             cmd.Parameters.Add(new SqlParameter("Correct_result", SqlDbType.Int));
             cmd.Parameters.Add(new SqlParameter("Correct_Type", SqlDbType.Int));
             cmd.Parameters.Add(new SqlParameter("Remark", SqlDbType.NVarChar));
             cmd.Parameters.Add(new SqlParameter("Standard", SqlDbType.NVarChar));
             cmd.Parameters.Add(new SqlParameter("CloseTime", SqlDbType.DateTime));
             cmd.Parameters.Add(new SqlParameter("FormStatus", SqlDbType.Int));
             cmd.Parameters.Add(new SqlParameter("ActualFormNO", SqlDbType.Int));
             cmd.Parameters.Add(new SqlParameter("ActualID", SqlDbType.Int));

             User creater = form.Creater;
             User applicant = form.Applicant;
             Item item = form.Item;

             cmd.Parameters["InstanceID"].Value = form.instanceID;
             cmd.Parameters["CorrectID"].Value = form.FormID;
             cmd.Parameters["Number"].Value = form.FormNO;
             cmd.Parameters["Creater"].Value = creater.EmpNO;
             cmd.Parameters["Applicant"].Value = applicant.EmpNO;
             cmd.Parameters["Create_date"].Value = form.ApplyDate;
             cmd.Parameters["Type"].Value = (int)form.FormType;
             cmd.Parameters["Reason"].Value = form.ApplyReason;
             cmd.Parameters["Fas_Type"].Value = (int)item.AssetType;
             cmd.Parameters["Fas_NO"].Value = item.AssetNO;
             cmd.Parameters["PR_NO"].Value = item.PRNO;
             if (item.BuyDate.HasValue)
                 cmd.Parameters["Buy_Date"].Value = item.BuyDate.Value;
             else
                 cmd.Parameters["Buy_Date"].Value = DBNull.Value;

             if (item.CurrencyType.HasValue)
                 cmd.Parameters["Buy_Currency"].Value = item.CurrencyType.ToString();
             else
                 cmd.Parameters["Buy_Currency"].Value = DBNull.Value;

             if (item.BuyAmount.HasValue)
                 cmd.Parameters["Buy_AMT"].Value = item.BuyAmount;
             else
                 cmd.Parameters["Buy_AMT"].Value = DBNull.Value;

             if (item.FirstCorrectTime.HasValue)
                 cmd.Parameters["First_Correct"].Value = item.FirstCorrectTime.Value;
             else
                 cmd.Parameters["First_Correct"].Value = DBNull.Value;
             if (item.NeedCorrectDate.HasValue)
                 cmd.Parameters["Correct_Date"].Value = item.NeedCorrectDate.Value;
             else
                 cmd.Parameters["Correct_Date"].Value = DBNull.Value;

             if (item.CorrectCurrencyType.HasValue)
                 cmd.Parameters["Exp_Currency"].Value = item.CorrectCurrencyType.ToString();
             else
                 cmd.Parameters["Exp_Currency"].Value = DBNull.Value;

             if (item.CorrectAmount.HasValue)
                 cmd.Parameters["Correct_AMT"].Value = item.CorrectAmount.Value;
             else
                 cmd.Parameters["Correct_AMT"].Value = DBNull.Value;
             if (item.CorrectCycle.HasValue)
                 cmd.Parameters["Correct_Period"].Value = (int)item.CorrectCycle.Value;
             else
                 cmd.Parameters["Correct_Period"].Value = DBNull.Value;
             if (item.CurrentStatus.HasValue)
                 cmd.Parameters["Status"].Value = (int)item.CurrentStatus.Value;
             else
                 cmd.Parameters["Status"].Value = DBNull.Value;
             if (item.CorrectResult.HasValue)
                 cmd.Parameters["Correct_result"].Value = (int)item.CorrectResult.Value;
             else
                 cmd.Parameters["Correct_result"].Value = DBNull.Value;

             //cmd.Parameters["Correct_Type"].Value = item.CorrectType.HasValue ? (int)item.CorrectType.Value : DBNull.Value;
             if (item.CorrectType.HasValue)
                 cmd.Parameters["Correct_Type"].Value = (int)item.CorrectType.Value;
             else
                 cmd.Parameters["Correct_Type"].Value = DBNull.Value;
             cmd.Parameters["Remark"].Value = item.Remark;
             cmd.Parameters["Standard"].Value = item.Standard;
             cmd.Parameters["ActualFormNO"].Direction = ParameterDirection.Output;
             cmd.Parameters["ActualID"].Direction = ParameterDirection.Output;

             if (form.CloseTime.HasValue)
                 cmd.Parameters["CloseTime"].Value = form.CloseTime.Value;
             else
                 cmd.Parameters["CloseTime"].Value = DBNull.Value;
             cmd.Parameters["FormStatus"].Value = (int)form.FormStatus;

             if (form.FormStatus != FormStatus.Draft)
             {
                 cmd.CommandText = "sp_SaveFormInfo";
                 cmd.CommandType = CommandType.StoredProcedure;
                 cmd.ExecuteNonQuery();
                 form.FormNO = (int)cmd.Parameters["ActualFormNO"].Value;
                 form.FormID = (int)cmd.Parameters["ActualID"].Value;
             }
             else
             {
                 string sql = "";
                 if (form.FormID > 0)
                 {
                     sql = "UPDATE Correct SET Creater = @Creater,Applicant = @Applicant,Create_Date = @Create_date,";
                     sql += "Type = @Type,Reason = @Reason,Fas_Type = @Fas_Type,Fas_NO = @Fas_NO,";
                     sql += "PR_NO = @PR_NO, Buy_Date = @Buy_Date, Buy_Currency = @Buy_Currency, Buy_AMT = @Buy_AMT,";
                     sql += "First_Correct = @First_Correct, Correct_Date = @Correct_Date, Exp_Currency = @Exp_Currency,";
                     sql += "Correct_AMT = @Correct_AMT, Correct_period = @Correct_period, Status = @Status, Correct_result = @Correct_result,";
                     sql += "Correct_Type = @Correct_Type, Remark = @Remark, Standard = @Standard WHERE CorrectID = @CorrectID";
                 }
                 else
                 {
                     sql = "INSERT INTO Correct(CorrectInstanceID,Number,Creater,Applicant,Create_Date,Type,Reason,";
                     sql += "Fas_Type,Fas_NO,PR_NO,Buy_Date,Buy_Currency,Buy_AMT,First_Correct,Correct_Date,Exp_Currency,";
                     sql += "Correct_AMT,Correct_period,Status,Correct_result,Correct_Type,Remark,Standard,CloseTime,FormStatus)";
                     sql += " VALUES(@InstanceID,0,@Creater,@Applicant,@Create_date,@Type,@Reason,";
                     sql += "@Fas_Type,@Fas_NO,@PR_NO,@Buy_Date,@Buy_Currency,@Buy_AMT,@First_Correct,@Correct_Date,@Exp_Currency,";
                     sql += "@Correct_AMT,@Correct_period,@Status,@Correct_result,@Correct_Type,@Remark,@Standard,NULL,@FormStatus)";
                 }
                 cmd.CommandText = sql;
                 cmd.CommandType = CommandType.Text;
                 cmd.ExecuteNonQuery();
             }

         }
         public void SaveAttachment(ApplyForm form, SqlCommand cmd)
         {
             if (form.Item.ItemAttachment.Count == 0)
                 return;

             foreach (Attach attach in form.Item.ItemAttachment)
             {
                 string sql = "INSERT INTO Tbl_Attachment(FormNO,SystemName,ClientName,Type) ";
                 sql += " values (@FormNO,@SystemName,@ClientName,@Type)";
                 cmd.Parameters.Clear();

                 cmd.Parameters.Add(new SqlParameter("FormNO", SqlDbType.Int));
                 cmd.Parameters.Add(new SqlParameter("SystemName", SqlDbType.VarChar));
                 cmd.Parameters.Add(new SqlParameter("ClientName", SqlDbType.NVarChar));
                 cmd.Parameters.Add(new SqlParameter("Type", SqlDbType.Int));

                 cmd.Parameters["FormNO"].Value = form.FormNO;
                 cmd.Parameters["SystemName"].Value = attach.SystemName;
                 cmd.Parameters["ClientName"].Value = attach.ClientName;
                 cmd.Parameters["Type"].Value = (int)attach.Type;

                 cmd.CommandType = CommandType.Text;
                 cmd.CommandText = sql;
                 cmd.ExecuteNonQuery();
             }

         }
         public DataTable GetNextFlowMailAddress(FormRouting routing)
         {
             DataSet ds = new DataSet();
             SqlDataAdapter da = new SqlDataAdapter();
             SqlParameter[] para = new SqlParameter[1];
             para[0] = SQLHelper.SQLHelper.CreateParameter("FormNO", SqlDbType.Int, routing.FormNO, ParameterDirection.Input);

             SQLHelper.SQLHelper.Fill(da, ds, "sp_GetFormMailAddress", CommandType.StoredProcedure, para);
             return ds.Tables[0];
         }
         
         public void RejectRouting(FormRouting routing, int intFlowNO,SqlCommand cmd)
         {
             cmd.Parameters.Clear();

             cmd.Parameters.Add(new SqlParameter("FormNO", SqlDbType.Int));
             cmd.Parameters.Add(new SqlParameter("FlowNO", SqlDbType.Int));
             cmd.Parameters.Add(new SqlParameter("BranchNO", SqlDbType.Int));
             cmd.Parameters.Add(new SqlParameter("ReceiverNO", SqlDbType.VarChar));
             cmd.Parameters.Add(new SqlParameter("ApproverNO", SqlDbType.VarChar));
             cmd.Parameters.Add(new SqlParameter("ApproverDate", SqlDbType.DateTime));
             cmd.Parameters.Add(new SqlParameter("ApproveResult", SqlDbType.Int));
             cmd.Parameters.Add(new SqlParameter("Remark", SqlDbType.NVarChar));
             cmd.Parameters.Add(new SqlParameter("RejectTo", SqlDbType.Int));

             cmd.Parameters["FormNO"].Value = routing.FormNO;
             cmd.Parameters["FlowNO"].Value = routing.FlowNO;
             cmd.Parameters["BranchNO"].Value = routing.BranchNO;
             cmd.Parameters["ReceiverNO"].Value = routing.Receiver.EmpNO;
             cmd.Parameters["ApproverNO"].Value = routing.Approver.EmpNO;
             cmd.Parameters["ApproverDate"].Value = routing.ApproverDate;
             cmd.Parameters["ApproveResult"].Value = (int) routing.ApproveResult.Value;
             cmd.Parameters["Remark"].Value = routing.Remark;
             cmd.Parameters["RejectTo"].Value = intFlowNO;

             cmd.CommandText = "sp_Reject";
             cmd.CommandType = CommandType.StoredProcedure;
             cmd.ExecuteNonQuery();
         }

         public void UpdateRouting(FormRouting routing,SqlCommand cmd)
         {
             cmd.Parameters.Clear();
             string sql = "update tbl_routing set ApproverNO = @ApproverNO,ApproverDate = @ApproverDate,";
             sql += "ApproveStatus = @Status,ApproveResult = @Result,Remark = @Remark where ";
             sql += "FormNO = @FormNO and FlowNO = @FlowNO and BranchNO = @BranchNO and ReceiverNO = @ReceiverNO";
             cmd.Parameters.Add(new SqlParameter("ApproverNO", SqlDbType.VarChar));
             cmd.Parameters.Add(new SqlParameter("ApproverDate", SqlDbType.DateTime));
             cmd.Parameters.Add(new SqlParameter("Status", SqlDbType.Int));
             cmd.Parameters.Add(new SqlParameter("Result", SqlDbType.Int));
             cmd.Parameters.Add(new SqlParameter("Remark", SqlDbType.NVarChar));
             cmd.Parameters.Add(new SqlParameter("FormNO", SqlDbType.Int));
             cmd.Parameters.Add(new SqlParameter("FlowNO", SqlDbType.Int));
             cmd.Parameters.Add(new SqlParameter("BranchNO", SqlDbType.Int));
             cmd.Parameters.Add(new SqlParameter("ReceiverNO", SqlDbType.VarChar));

             User approver = routing.Approver;
             cmd.Parameters["ApproverNO"].Value = approver.EmpNO;
             cmd.Parameters["ApproverDate"].Value = routing.ApproverDate;
             cmd.Parameters["Status"].Value = (int)routing.ApproveStatus;
             cmd.Parameters["Result"].Value = (int)routing.ApproveResult;
             cmd.Parameters["Remark"].Value = routing.Remark;
             cmd.Parameters["FormNO"].Value = routing.FormNO;
             cmd.Parameters["FlowNO"].Value = routing.FlowNO;
             cmd.Parameters["BranchNO"].Value = routing.BranchNO;
             cmd.Parameters["ReceiverNO"].Value = routing.Receiver.EmpNO;

             cmd.CommandText = sql;
             cmd.CommandType = CommandType.Text;
             cmd.ExecuteNonQuery();
         }

         #region  根據表單的ID得到表單的詳細信息
         public ApplyForm GetFormInfoById(int id)
         {
             DataSet ds = GetFormInfo(id);
             ApplyForm form = GenerateForm(ds.Tables["form"], ds.Tables["attach"], ds.Tables["routing"]); 
             return form;
         }

         public DataSet GetFormInfo(int id)
         {
             DataSet ds = new DataSet();
             SqlDataAdapter da = new SqlDataAdapter();
             SqlParameter[] para = new SqlParameter[1];
             para[0] = SQLHelper.SQLHelper.CreateParameter("ID", SqlDbType.Int, id, ParameterDirection.Input);
             da.TableMappings.Add("Table", "form");
             da.TableMappings.Add("Table1", "attach");
             da.TableMappings.Add("Table2", "routing");
             SQLHelper.SQLHelper.Fill(da, ds, "sp_GetApplyFormByID", CommandType.StoredProcedure, para);

             return ds;
         }
         public ApplyForm GenerateForm(DataTable dt,DataTable dtAttachment,DataTable dtRouting)
         {
             ApplyForm form = new ApplyForm();

             User_dal dal = new User_dal();
             User creater = dal.GetUserInfo_dal(dt.Rows[0]["Creater"].ToString());
             User applicant = dal.GetUserInfo_dal(dt.Rows[0]["Applicant"].ToString());

             form.instanceID = (Guid)dt.Rows[0]["CorrectInstanceID"];
             form.FormID = int.Parse(dt.Rows[0]["CorrectID"].ToString());
             form.FormNO = int.Parse(dt.Rows[0]["Number"].ToString());
             form.Creater = creater;
             form.Applicant = applicant;
             form.ApplyDate = Convert.ToDateTime(dt.Rows[0]["Create_date"].ToString());
             form.FormType = (FormType)int.Parse(dt.Rows[0]["type"].ToString());
             form.ApplyReason = dt.Rows[0]["Reason"].ToString();
             form.FormStatus = (FormStatus)int.Parse(dt.Rows[0]["FormStatus"].ToString());
             if (dt.Rows[0]["CloseTime"].ToString() != "")
                 form.CloseTime = Convert.ToDateTime(dt.Rows[0]["CloseTime"].ToString());
             form.Item = GenerateItem(dt,dtAttachment);

             form.FormRoutingHistory = new List<FormRouting>();
             foreach (DataRow row in dtRouting.Rows)
             {
                 form.FormRoutingHistory.Add(GenerateRouting(row));
             }

             return form;
         }
         public Item GenerateItem(DataTable dt,DataTable dtAttach)
         {
             Item item = new Item();
             item.AssetNO = dt.Rows[0]["Fas_NO"].ToString();
             item.AssetType = (AssetType)int.Parse(dt.Rows[0]["Fas_Type"].ToString());
             item.Brand = dt.Rows[0]["Brand"].ToString();
             if (dt.Rows[0]["Buy_AMT"].ToString() != "")
                item.BuyAmount = Convert.ToDouble(dt.Rows[0]["Buy_AMT"].ToString());
             if (dt.Rows[0]["Buy_date"].ToString() != "")
                item.BuyDate = Convert.ToDateTime(dt.Rows[0]["Buy_date"].ToString());
             item.ChiName = dt.Rows[0]["ChiName"].ToString();
             if (dt.Rows[0]["Correct_AMT"].ToString() != "")
                item.CorrectAmount = Convert.ToDouble(dt.Rows[0]["Correct_AMT"].ToString());
             if (dt.Rows[0]["Buy_currency"].ToString() != "")
                item.CurrencyType = (CurrencyType)Enum.Parse(typeof(CurrencyType), dt.Rows[0]["Buy_currency"].ToString());
             if (dt.Rows[0]["Exp_currency"].ToString() != "")
                item.CorrectCurrencyType = (CurrencyType) Enum.Parse(typeof(CurrencyType), dt.Rows[0]["Exp_currency"].ToString());
             if (dt.Rows[0]["Correct_period"].ToString() != "")
                item.CorrectCycle = (CorrectCycle)int.Parse(dt.Rows[0]["Correct_period"].ToString());
             if (dt.Rows[0]["Correct_result"].ToString() != "")
                item.CorrectResult = (CorrectResult)int.Parse(dt.Rows[0]["Correct_result"].ToString());
             if (dt.Rows[0]["Correct_Type"].ToString() != "")
                item.CorrectType = (CorrectType)int.Parse(dt.Rows[0]["Correct_Type"].ToString());
             if (dt.Rows[0]["status"].ToString() != "")
                item.CurrentStatus = (AssetStatus)int.Parse(dt.Rows[0]["status"].ToString());
             if (dt.Rows[0]["status"].ToString() != "")
                item.FirstCorrectTime = Convert.ToDateTime(dt.Rows[0]["First_Correct"].ToString());

             User_dal dal = new User_dal();
             item.Keeper = dal.GetUserInfo_dal(dt.Rows[0]["KeeperNO"].ToString());
             item.ModelNO = dt.Rows[0]["ModelNO"].ToString();
             if (dt.Rows[0]["Correct_date"].ToString() != "")
                item.NeedCorrectDate = Convert.ToDateTime(dt.Rows[0]["Correct_date"].ToString());
             item.PRNO = dt.Rows[0]["PR_NO"].ToString();
             item.Remark = dt.Rows[0]["remark"].ToString();
             item.Spec = dt.Rows[0]["Spec"].ToString();
             item.Standard = dt.Rows[0]["standard"].ToString();

             item.ItemAttachment = new List<Attach>();
             foreach (DataRow row in dtAttach.Rows)
             {
                 item.ItemAttachment.Add(GenerateAttach(row));
             }

             return item;
         }
         public Attach GenerateAttach(DataRow row)
         {
             Attach attach = new Attach();
             attach.FormNO = int.Parse(row["FormNO"].ToString());
             attach.SystemName = row["SystemName"].ToString();
             attach.ClientName = row["ClientName"].ToString();
             attach.Type = (AttachmentType)int.Parse(row["Type"].ToString());                  
             return attach;
         }
         public FormRouting GenerateRouting(DataRow row)
         {
             FormRouting routing = new FormRouting();
             routing.FormNO = int.Parse(row["FormNO"].ToString());
             routing.FlowNO = int.Parse(row["FlowNO"].ToString());
             routing.BranchNO = int.Parse(row["BranchNO"].ToString());

             User Approver = new User();
             User Receiver = new User();
             Approver.EmpChineseName = row["Approver"].ToString();
             Approver.EmpNO = row["ApproverNO"].ToString();
             Receiver.EmpChineseName = row["Receiver"].ToString();
             Receiver.EmpNO = row["ReceiverNO"].ToString();
             routing.Approver = Approver;
             routing.Receiver = Receiver;
             routing.FlowName = row["FlowName"].ToString();
             if (row["ReceiveDate"].ToString() != "")
                routing.ReceiveDate = Convert.ToDateTime(row["ReceiveDate"].ToString());
             routing.AgentNO = row["AgentNO"].ToString();
             if (row["ApproverDate"].ToString() != "")
                 routing.ApproverDate = Convert.ToDateTime(row["ApproverDate"].ToString());
             if (row["ApproveStatus"].ToString() != "")
                routing.ApproveStatus = (ApproveStatus)int.Parse(row["ApproveStatus"].ToString());
             if (row["ApproveResult"].ToString() != "")
                 routing.ApproveResult = (ApproveResult)int.Parse(row["ApproveResult"].ToString());
             routing.Remark = row["Remark"].ToString();
             return routing;
         }
         #endregion

         //拒绝的时候需要根据单号得到流程
         public List<FormRouting> GetDistinctRouting(int FormNO)
         {
             DataSet ds = new DataSet();
             SqlDataAdapter da = new SqlDataAdapter();
             SqlParameter[] para = new SqlParameter[1];
             para[0] = SQLHelper.SQLHelper.CreateParameter("FormNO", SqlDbType.Int, FormNO, ParameterDirection.Input);
             SQLHelper.SQLHelper.Fill(da, ds, "sp_GetFormDistinctRouting", CommandType.StoredProcedure, para);

             List<FormRouting> list = new List<FormRouting>();
             foreach (DataRow row in ds.Tables[0].Rows)
             {
                 FormRouting routing = new FormRouting();
                 User receiver = new User();
                 receiver.EmpChineseName = row["Receiver"].ToString();
                 routing.Receiver = receiver;
                 routing.FlowNO = int.Parse(row["FlowNO"].ToString());
                 routing.FlowName = row["FlowName"].ToString();
                 list.Add(routing);
             }
             return list;
         }
    }
}
